home *** CD-ROM | disk | FTP | other *** search
/ Info-Mac 1993 May / Info-Mac_II_May_1993.to_.sit / Info-Mac II (May 1993).toast / Technical / Binhex 4.0 Specs.txt next >
Internet Message Format  |  1992-08-29  |  9KB

  1. Date: Tue, 13 Aug 91 09:18:07 WST 
  2. From: Peter N Lewis <peter@cujo.curtin.edu.au>
  3. Subject: info-mac/tech/binhex-definition.txt 
  4.  
  5. Hi All,
  6.    This is a definition of the BinHex 4.0 standard as I see it.  When I
  7. first tried to write DeHQX, I had to post several questions to the net
  8. to get a full definition of this standard.  Hopefully this file will
  9. make it easier for anyone who wants to add BinHex compatability to there
  10. application.
  11.  
  12. Have fun,
  13.    Peter <Lewis_P@cc.curtin.edu.au>
  14.  
  15. __________________________________________________________________________
  16. BinHex 4.0 Definition by Peter N Lewis, Aug 1991.
  17.  
  18.     For a long time BinHex 4.0 has been the standard for ASCII encoding of
  19. Macintosh files.  To my knowledge, there has never been a full definition
  20. of this format.  Info-Mac had an informal definition of the format, but
  21. this lacked a description of the CRC calculation, as well as being vague in
  22. some areas.  Hopefully this document will fully define the BinHex 4.0
  23. standard, and allow more programmers to fully implement it.  Note, however,
  24. that this definition is how I see the BinHex standard, and since I had no
  25. part whatsoever in defining it initially, this document can have no real
  26. claim to being the one true definition.  If anyone feels that I have not
  27. got the facts straight, or that it is ambiguous in any details, please
  28. contact me at the address at the bottom of this document.
  29.  
  30. Format:
  31.  
  32. It is necessary to distinguish between the encoding format and decoding
  33. format since we wish to allow all decoders to read all versions of the
  34. BinHex format, while trying to reduce the variation in encoding.
  35.  
  36. All numbers are decimal unless they have the format 0xFF, in which case
  37. they are hex.
  38.  
  39. <tab> is a tab, character value 9.
  40. <lf>  is a linefeed, character value 10.
  41. <cr> is a carriage return, character value 13.
  42. <spc> is a space, character value 32.
  43. <return> means to the encoder:
  44.     The sequence <cr> <lf>.  Either (but not both) may be omitted.  Use
  45. whatever is appropriate for your system (<cr> for Mac, <lf> for Unix,
  46. <cr><lf> for MS-DOS).
  47. <return> means to the decoder:
  48.     Any sequence of zero or more of <cr>, <lf>, <tab>, <spc>.  (The <tab> 
  49. and <spc> are required because some old programs produced these characters).
  50. For example, <cr> <tab> <lf> <spc> <lf> <cr> would be perfectly acceptable.
  51.  
  52. A hqx file begins with a description which should be ignored by the decoder
  53. (and generally left blank by encoding software).  The hqx file proper
  54. consists of the sequence:
  55.  
  56. <start-of-line>(This<spc>file<spc>must<spc>be<spc>converted<spc>with<spc>
  57. BinHex<spc>4.0)<return>:<hqx-file>:<return>
  58.  
  59. When encoding, DO NOT mess with the "(This file..." string.  There are a
  60. large number of automated programs that use it, and they may stumble over
  61. anything other than this exact string.
  62.  
  63. When decoding, you should only check the string up to "with BinHex", then
  64. skip until either a <cr> or <lf>, then skip <return> characters, and check
  65. for the colon.  Also, be careful with the <start-of-line>, this can be
  66. either a <return> character, or the start of the file.  Some old programs
  67. produced an extra exclamation mark (!) immediately before the final colon
  68. (:).  When decoding, after all data is read, skip any <return> characters,
  69. and then allow a single optional exclamation mark (and then skip <returns>
  70. again) before checking for the terminating colon.  Don't check for a
  71. <return> after the colon.
  72.  
  73. <hqx-file> is a sequence of 6-bit encoded characters.
  74.  
  75. When encoding, a <return> should be inserted after every 64 characters. 
  76. The first character should follow immediately after the first colon
  77. (without a <return>), and the first line should be 64 characters long,
  78. (unless its also the last line obviously) including the colon.  The 
  79. final colon should go on the same line as the last character and there 
  80. should be a return after it.  Thus, the final line must be between 2 and 
  81. 65 (inclusive) characters long.
  82.  
  83. When decoding, lines of any length should be accepted, and <return>
  84. characters should be ignored everywhere (before and after the first colon,
  85. between any two hqx characters, and before the trailing colon.
  86.  
  87. This string defines the valid characters, in order:
  88. !"#$%&'()*+,-012345689@ABCDEFGHIJKLMNPQRSTUVXYZ[`abcdefhijklmpqr
  89. (ie, ! is 0, " is 1, ..., r is 63).
  90.  
  91. When decoding, any character other that those 64 and the <return>
  92. characters indicates an error and the user should be notified of such.
  93.  
  94. The format also supports run length encoding, where the character to be
  95. repeated is followed by a 0x90 byte then the repeat count.  For example,
  96. FF9004 means repeat 0xFF 4 times.  The special case of a repeat count of
  97. zero means it's not a run, but a literal 0x90.  2B9000 => 2B90.
  98.  
  99. *** Note: the 9000 can be followed by a run, which means to repeat the 0x90
  100. (not the character previous to that).  That is, 2B90009005 means a 2B9090909090.
  101.  
  102. Before encoding into RLE, 6-bits (or after if decoding), the file is
  103. formatted into a header, data fork, and resource fork.  All three are
  104. terminated with a two byte CRC.
  105.  
  106. The header consists of a one byte name length, then the file name, then a
  107. one byte 0x00.  The rest of the header is 20 bytes long, and contains the
  108. file type, creator, file flags, data and resource lengths, and the two byte
  109. CRC value for the header.
  110.  
  111. When encoding, the flags should be copied directly from the Finder Info. 
  112. When decoding, the OnDesk, Invisible, and Initted flags should be cleared.
  113.  
  114. Also, when decoding, the file name should be validated for the given OS. 
  115. For example, a Mac program should replace a full stop (.) at the front of a
  116. file name with a bullet (option-8), and should replace colons (:) with
  117. dashes (-) and, if running under A/UX, slashes should be replaced by dashes
  118. (-).
  119.  
  120. The data fork and resource fork contents follow in that order.  If a fork
  121. is empty, there will be no bytes of contents and the checksum will be two
  122. bytes of zero.
  123.  
  124. So the decoded data between the first and last colon (:) looks like:
  125.  
  126.      1       n       4    4    2    4    4   2    (length)
  127.     +-+---------+-+----+----+----+----+----+--+
  128.     |n| name... |0|TYPE|AUTH|FLAG|DLEN|RLEN|HC|    (contents)
  129.     +-+---------+-+----+----+----+----+----+--+
  130.  
  131.             DLEN             2    (length)
  132.     +--------------------------------------+--+
  133.     |    DATA FORK               |DC|    (contents)
  134.     +--------------------------------------+--+
  135.  
  136.             RLEN             2    (length)
  137.     +--------------------------------------+--+
  138.     |    RESOURCE FORK               |RC|    (contents)
  139.     +--------------------------------------+--+
  140.  
  141. CRCs:
  142.  
  143. BinHex 4.0 uses a 16-bit CRC with a 0x1021 seed.  The general algorithm is
  144. to take data 1 bit at a time and process it through the following:
  145.  
  146. 1) Take the old CRC (use 0x0000 if there is no previous CRC) and shift it
  147. to the left by 1.
  148.  
  149. 2) Put the new data bit in the least significant position (right bit).
  150.  
  151. 3) If the bit shifted out in (1) was a 1 then xor the CRC with 0x1021.
  152.  
  153. 4) Loop back to (1) until all the data has been processed.
  154.  
  155. Or in pseudo pascal:
  156.  
  157. var crc:integer; { init to zero at the beginning of each of the three forks }
  158.  
  159. procedure CalcCRC (v: integer);  { 0<=v<=255 }
  160.     var
  161.         temp: boolean;
  162.         i: integer;
  163. begin
  164.     for i := 1 to 8 do begin
  165.         temp := (crc AND 0x8000) <> 0;
  166.         crc := (crc << 1) OR (v >> 7);
  167.         if temp then
  168.             crc := crc XOR 0x1021;
  169.         v := (v << 1) AND 0xFF;
  170.     end;
  171. end;
  172.  
  173. When encoding, include two bytes of 0x00 where the CRC should be, and use
  174. them in the calculation of the CRC before writing it to the file. 
  175. Similarly, when decoding, calculate the CRC over all the bytes in the fork
  176. (header, data, or resource) except the last two CRC bytes, then continue
  177. the CRC calculation with two 0x00 bytes, then compare it to the CRC stored
  178. in the file.
  179.  
  180. Parts:
  181.  
  182. If you wish to support segmented files in the same way that
  183. comp.binaries.mac files are segmented,  use the following line to note the
  184. end of a hqx part:
  185. <return>--- end of part NN ---<return>
  186.  
  187. Note: When decoding, it is only necessary to check for "<return>--- end of
  188. part".
  189.  
  190. Recommence decoding (either later in this file, or in the next file) when 
  191. you encounter the line:
  192. <return>---<return>
  193.  
  194. Note:  In this case, when decoding, demand either a <cr> or <lf> both
  195. immediately before, and immediately after the ---.  It is unfortunate that
  196. this sequence is in fact valid hqx data, but there should not be any 
  197. problems with this.
  198.  
  199. Sources to refer to:
  200.  
  201. FTP from sumex-aim.stanford.edu
  202.     A file called hqx-format.txt which I can no longer find.
  203.     info-mac/unix/xbin
  204.     info-mac/unix/mcvert
  205.     info-mac/source/pascal/dehqx
  206.  
  207. Author: 
  208.  
  209. Peter N Lewis <Lewis_P@cc.curtin.edu.au>
  210. 10 Earlston way,
  211. Booragoon 6154 WA,
  212. AUSTRALIA
  213.  
  214. Contributors:
  215.  
  216. Roland Mansson <Roland.Mansson@LDC.lu.se>
  217. Steve Dorner <dorner@pequod.cso.uiuc.edu>
  218. Sak Wathanasin <sw@network-analysis-ltd.co.uk>
  219. Dave Green <daveg@Apple.com>
  220. Tom Coradeschi <tcora@PICA.ARMY.MIL>
  221. Howard Haruo Fukuda <physi-hf@garnet.berkeley.edu>
  222. Michael Fort <mfort@ub.d.umn.edu>
  223. Dave Johnson <ddj%brown@csnet-relay.ARPA>
  224.  
  225. Note: I attempted to contact all these people, but only a few replied. 
  226. Some of them may not wish to be associated with this document, and
  227. certainly they should not be seen as endorsing it in any way.  Obviously,
  228. any errors in this document are my own!
  229.  
  230.  
  231.